<template>
	<view class="setUp">
		<view class="list">
			<view class="head_img">
				<view class="tips">店铺头像</view>
				<view class="texts" @click="chooseFile">
					<image :src="businessMsg.logo" mode=""></image>
					<uni-icons type="forward" size="16" color="#999999"></uni-icons>
				</view>
			</view>
			<view class="" @click="onSkip(`/package/mine/setUp/setNickName?pageType=店铺名称&bid=${bid}`)">
				<view class="tips">店铺名称</view>
				<view class="texts">
					<text class="text_val">{{businessMsg.name}}</text>
					<uni-icons type="forward" size="16" color="#999999"></uni-icons>
				</view>
			</view>
			<view class="" @click="onSkip(`/package/mine/setUp/openTime?pagetype=营业时间&bid=${bid}`)">
				<view class="tips">营业时间</view>
				<view class="texts">
					<text class="text_val">{{businessMsg.start_hours}}-{{businessMsg.end_hours}}</text>
					<uni-icons type="forward" size="16" color="#999999"></uni-icons>
				</view>
			</view>

		</view>
		<view class="list">
			<view class="" @click="handleChooseLoaction">
				<view class="tips">店铺位置</view>
				<view class="texts">
					<text class="text_val addressText">{{businessMsg.address}}</text>
					<uni-icons type="forward" size="16" color="#999999"></uni-icons>
				</view>
			</view>
			<view class="" @click="onSkip(`/package/mine/setUp/openTime?pagetype=店铺电话&bid=${bid}`)">
				<view class="tips">店铺电话</view>
				<view class="texts">
					<text class="text_val">{{businessMsg.tel}}</text>
					<uni-icons type="forward" size="16" color="#999999"></uni-icons>
				</view>
			</view>
		</view>
		
		
		<view class="list">
			<view class="" @click="gotoJinJian">
				<view class="tips">进件资料</view>
				<view class="texts">
					<text class="text_val"></text>
					<uni-icons type="forward" size="16" color="#999999"></uni-icons>
				</view>
			</view>
		</view>
		
		<view class="list" v-if="false">
			<view class="" @click="gotoJieSuan">
				<view class="tips">结算管理</view>
				<view class="texts">
					<text class="text_val"></text>
					<uni-icons type="forward" size="16" color="#999999"></uni-icons>
				</view>
			</view>
		</view>
		
		<view class="list">
			<view class="">
				<view class="tips">播报管理</view>
				<view class="texts">
					<switch :checked="yuying_checked" @change="switch1Change" />
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getStoreMsg,
		setStoreMsg,
		getPersonalCenter,
	} from "@/api/index.js"
	
	import { hex_md5 } from "@/utils/md5.js";
	export default {
		data() {
			return {
				isAgency: getApp().globalData.isAgency,
				bid: '',
				yuying_checked: false,
				businessMsg: {}
			}
		},
		onLoad(opt) {
			this.bid = opt.bid
		},
		onShow() {
			this.onGetStoreMsg()
		},
		methods: {
			async switch1Change(e) {
				this.yuying_checked = e.target.value
				
				const res = await setStoreMsg(`s=/ApiBusiness/dosetyuying`, {
					bid: this.bid
				})
				
				console.log(res)
				
				const {
					data,
					msg,
					status
				} = res.data
				this.showToast(msg)
				if (status == 1) {
					setTimeout(() => {
						this.onGetStoreMsg()
					}, 2000)
					console.log(res.data)
				}
			},
			async gotoJinJian(){
				const res = await getPersonalCenter('s=/ApiZwyRequest/userinfo', {}, true)
				const {
					data,
					status,
					msg
				} = res.data
				var tel = '16e5e781970fd16d' + data.tel
				var url = encodeURIComponent(`${getApp().globalData.BASE_URL}/?s=/ApiUpload/sybmercinfo&mobile=${data.tel}&token=${hex_md5(tel)}`)
				this.onSkip(
					`/business/pages/business_tab/web?url=${url}`
				)
			},
			async gotoJieSuan(){
				const res = await getPersonalCenter('s=/ApiZwyRequest/userinfo', {}, true)
				const {
					data,
					status,
					msg
				} = res.data
				var tel = '16e5e781970fd16d' + data.tel
				var url = encodeURIComponent(`${getApp().globalData.BASE_URL}/?s=/ApiUpload/sybquerysettle&mobile=${data.tel}&token=${hex_md5(tel)}`)
				this.onSkip(
					`/business/pages/business_tab/web?url=${url}`
				)
			},
			async onGetPersonalCenter() {
				const res = await setStoreMsg('s=/ApiZwyRequest/userinfo')
				const {
					data,
					status,
					msg
				} = res.data
				if (status == 1) {
					this.centerMsg = data
					this.merchant = this.centerMsg.business
			
				} else {
					this.showToast(msg)
				}
			},
			// 修改店铺信息
			async onSetStoreMsg(val) {
				const res = await setStoreMsg(`s=/ApiBusiness/setinfo`, val)
				const {
					data,
					msg,
					status
				} = res.data
				this.showToast(msg)
				if (status == 1) {
					setTimeout(() => {
						this.onGetStoreMsg()
					}, 2000)
					console.log(res.data)
				}
			},
			// 获取店铺信息
			async onGetStoreMsg() {
				const res = await getStoreMsg(`s=/ApiBusiness/setinfo&bid=${this.bid}`)
				const {
					data,
					msg,
					status
				} = res.data
				console.log(res)
				if (status == 1) {
					this.businessMsg = data
					this.yuying_checked = data.yuying_status == 1;
					console.log(res.data)
				} else {
					this.showToast(msg)
				}
			},
			// 选择店铺位置
			handleChooseLoaction(type) {
				uni.chooseLocation({
					success: (res) => {
						const {
							longitude,
							address,
							latitude,
							name
						} = res
						let data = {
							bid: this.bid,
							address,
							longitude,
							latitude
						}
						this.onSetStoreMsg(data)
					},
					fail: (err) => {
						console.log(err);
					},
				});
			},
			//上传图片
			async chooseFile() {
				uni.chooseImage({
					count: 1,
					sourceType: ['album'],
					success: (res) => {
						uni.showLoading({
							title: '上传中...'
						})
						this.upImg(res.tempFilePaths[0])

					}
				});
			},
			upImg(path) {
				let BASE_URL = getApp().globalData.BASE_URL
				let platform = 'app'
				// #ifdef H5
				if (this.isAgency) {
					platform = 'h5'
					BASE_URL = '/api'
				}
				// #endif
				uni.uploadFile({
					url: `${BASE_URL}?s=/ApiImageupload/uploadImg&aid=1&platform=${platform} `,
					filePath: path,
					name: 'file',
					// #ifndef H5
					header: {
						'content-type': 'multipart/form-data'
					},
					// #endif
					success: (res) => {
						console.log(res, "上传成功")
						const data = JSON.parse(res.data);
						const {
							msg,
							status,
							url
						} = data
						if (status == 1) {
							this.businessMsg.logo = url
							this.uploadHeadimg(url)
						}
						uni.hideLoading();
					}
				});
			},
			// 提交头像-暂无
			async uploadHeadimg(headimg) {
				let data = {
					bid: this.bid,
					logo: headimg
				}
				this.onSetStoreMsg(data)
			}
		}
	}
</script>

<style>
	page {
		background-color: #f7f7f7;
	}
</style>
<style lang="scss">
	.setUp {
		padding-top: 40upx;

		.list {
			margin-bottom: 30upx;

			>view {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 40upx 30upx;
				box-sizing: border-box;
				background-color: #fff;
				border-bottom: 1px solid #f7f7f7;

				.tips {
					font-size: 32upx;
				}

				.texts {
					font-size: 32upx;
					color: #666666;
					display: flex;
					align-items: center;

					.text_val {

						color: #999;
						margin-right: 20upx;
					}

					.addressText {
						width: 50vw;
						white-space: nowrap;
						text-overflow: ellipsis;
						overflow: hidden;
					}
				}
			}

			.head_img {
				padding: 20upx 30upx;
				box-sizing: border-box;

				image {
					width: 80upx;
					height: 80upx;
					border-radius: 10rpx;
					margin-right: 20upx;
				}
			}

		}

		.btn {
			width: 100%;
			height: 96upx;
			line-height: 96upx;
			text-align: center;
			color: #41644A;
			font-size: 32upx;
			font-weight: bold;
			background-color: #fff;
		}
	}
</style>